<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);

$SIZE = 1;
$page = MiscUtils::getParam('p', 1);
$items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), NULL, NULL, NULL, $page, $SIZE, NULL)->d;

if (count($items) == 0) {
  $obj = new stdClass();
  $obj->url = '';
  $obj->file = '';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.form4 {
  background-color: #e9e9e9;
  border: 1px solid #666;
  color: #000;
  padding: 2px;
  padding-bottom: 20px;
  cursor: pointer;
  display: inline-block;
  margin: 2px;
  font-weight: bold;
}
table.form4 td {
  padding: 0;
  spacing: 0;
}
</style>
</head>
<body>
<div>Width x Height : 950 x 90</div>
<div id="spanButtonPlaceHolder"></div>
<div id="cancelBtnId" style="display: none;"></div>
<div id="img"></div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/myupload2.js"></script>
<script type="text/javascript" src="js/jquery.prettyPhoto.js" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="css/prettyPhoto.css" media="screen" charset="utf-8" />

<script type="text/javascript">
  var _items = JSON.parse('<?php echo addslashes(json_encode($items)); ?>');
  $(function(){
    new MyUpload('spanButtonPlaceHolder', '<?php echo basename(__FILE__, '.php'); ?>.upload.php', function(msgs) {
      location.reload();
    }, {
      dir: '../../../',
      button_image_url: 'images/browse.png',
      button_width: 94,
      button_height: 25,
      file_upload_limit: 1,
      file_queue_limit: 1,
      file_size_limit: '8MB',
      file_types: '*.jpg;*.jpeg;*.png;*.gif',
      file_types_description: 'Image Files',
      cancelBtnId: 'cancelBtnId',
      post_params: {
        id: _items[0].id
      },
      debug: 1
    });
    $('#img')[0].appendChild(__get_img(_items[0]));
    __set_pp();
  });

  function __get_img(img_obj) {
    var table, tr, td, elm, a, img;
    table = document.createElement('table');
    table.className = 'form4';
    table.cellPadding = 0;
    table.cellSpacing = 0;
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.textAlign = 'right';
    td.className = 'img';
    elm = document.createElement('img');
    elm.src = 'images/del.png';
    elm.onclick = function() {
      var r = window.confirm('Are You Sure?');
      if (r) {
        $.ajax({
          type: "POST",
          url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
          data: "action=del_img&q=" + encodeURIComponent(JSON.stringify(img_obj)),
          success: function(msg){
            _images = JSON.parse(msg);
            var obj, elm;
            obj = JSON.parse(msg);
            elm = document.getElementById('img' + obj.id);
            MyUtils.removeChildElements(elm);
            elm.appendChild(__get_img(obj));
            __set_pp();
          }
        });
      }
    };
    td.appendChild(elm);
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    a = document.createElement('a');
    a.rel = 'prettyPhoto[gallery1]';
    if (img_obj.file != '') {
      a.href = img_obj.file;
    } else {
      a.href = 'images/noimg.png';
    }
    td.appendChild(a);
    img = document.createElement('img');
    if (img_obj.file != '') {
      img.src = img_obj.file;
    } else {
      img.src = 'images/noimg.png';
    }
    img.style.width = '950px';
    a.appendChild(img);
    return table;
  }

  function __set_pp() {
    $("a[rel^='prettyPhoto']").prettyPhoto({
      social_tools: ''
    });
  }
</script>
</body>
</html>